/*   1:    */ package nl.siegmann.epublib.html.htmlcleaner;
/*   2:    */ 
/*   3:    */ import java.util.Iterator;
/*   4:    */ import java.util.List;
/*   5:    */ import java.util.Map;
/*   6:    */ import java.util.Map.Entry;
/*   7:    */ import java.util.Set;
/*   8:    */ import javax.xml.stream.XMLStreamException;
/*   9:    */ import javax.xml.stream.XMLStreamWriter;
/*  10:    */ import org.htmlcleaner.CleanerProperties;
/*  11:    */ import org.htmlcleaner.CommentNode;
/*  12:    */ import org.htmlcleaner.ContentNode;
/*  13:    */ import org.htmlcleaner.EndTagToken;
/*  14:    */ import org.htmlcleaner.TagNode;
/*  15:    */ 
/*  16:    */ public class XmlSerializer
/*  17:    */ {
/*  18:    */   protected CleanerProperties props;
/*  19:    */   
/*  20:    */   public XmlSerializer(CleanerProperties props)
/*  21:    */   {
/*  22: 21 */     this.props = props;
/*  23:    */   }
/*  24:    */   
/*  25:    */   public void writeXml(TagNode tagNode, XMLStreamWriter writer)
/*  26:    */     throws XMLStreamException
/*  27:    */   {
/*  28: 42 */     serialize(tagNode, writer);
/*  29:    */     
/*  30: 44 */     writer.flush();
/*  31:    */   }
/*  32:    */   
/*  33:    */   protected void serializeOpenTag(TagNode tagNode, XMLStreamWriter writer)
/*  34:    */     throws XMLStreamException
/*  35:    */   {
/*  36: 48 */     String tagName = tagNode.getName();
/*  37:    */     
/*  38: 50 */     writer.writeStartElement(tagName);
/*  39: 51 */     Map tagAtttributes = tagNode.getAttributes();
/*  40: 52 */     for (Iterator it = tagAtttributes.entrySet().iterator(); it.hasNext();)
/*  41:    */     {
/*  42: 53 */       Map.Entry entry = (Map.Entry)it.next();
/*  43: 54 */       String attName = (String)entry.getKey();
/*  44: 55 */       String attValue = (String)entry.getValue();
/*  45: 57 */       if ((this.props.isNamespacesAware()) || ((!"xmlns".equals(attName)) && (!attName.startsWith("xmlns:")))) {
/*  46: 60 */         writer.writeAttribute(attName, attValue);
/*  47:    */       }
/*  48:    */     }
/*  49:    */   }
/*  50:    */   
/*  51:    */   protected void serializeEmptyTag(TagNode tagNode, XMLStreamWriter writer)
/*  52:    */     throws XMLStreamException
/*  53:    */   {
/*  54: 65 */     String tagName = tagNode.getName();
/*  55:    */     
/*  56: 67 */     writer.writeEmptyElement(tagName);
/*  57: 68 */     Map tagAtttributes = tagNode.getAttributes();
/*  58: 69 */     for (Iterator it = tagAtttributes.entrySet().iterator(); it.hasNext();)
/*  59:    */     {
/*  60: 70 */       Map.Entry entry = (Map.Entry)it.next();
/*  61: 71 */       String attName = (String)entry.getKey();
/*  62: 72 */       String attValue = (String)entry.getValue();
/*  63: 74 */       if ((this.props.isNamespacesAware()) || ((!"xmlns".equals(attName)) && (!attName.startsWith("xmlns:")))) {
/*  64: 77 */         writer.writeAttribute(attName, attValue);
/*  65:    */       }
/*  66:    */     }
/*  67:    */   }
/*  68:    */   
/*  69:    */   protected void serializeEndTag(TagNode tagNode, XMLStreamWriter writer)
/*  70:    */     throws XMLStreamException
/*  71:    */   {
/*  72: 82 */     writer.writeEndElement();
/*  73:    */   }
/*  74:    */   
/*  75:    */   protected void serialize(TagNode tagNode, XMLStreamWriter writer)
/*  76:    */     throws XMLStreamException
/*  77:    */   {
/*  78: 87 */     if (tagNode.getChildren().isEmpty())
/*  79:    */     {
/*  80: 88 */       serializeEmptyTag(tagNode, writer);
/*  81:    */     }
/*  82:    */     else
/*  83:    */     {
/*  84: 90 */       serializeOpenTag(tagNode, writer);
/*  85:    */       
/*  86: 92 */       List tagChildren = tagNode.getChildren();
/*  87: 93 */       for (Iterator childrenIt = tagChildren.iterator(); childrenIt.hasNext();)
/*  88:    */       {
/*  89: 94 */         Object item = childrenIt.next();
/*  90: 95 */         if (item != null) {
/*  91: 96 */           serializeToken(item, writer);
/*  92:    */         }
/*  93:    */       }
/*  94: 99 */       serializeEndTag(tagNode, writer);
/*  95:    */     }
/*  96:    */   }
/*  97:    */   
/*  98:    */   private void serializeToken(Object item, XMLStreamWriter writer)
/*  99:    */     throws XMLStreamException
/* 100:    */   {
/* 101:105 */     if ((item instanceof ContentNode)) {
/* 102:106 */       writer.writeCharacters(((ContentNode)item).getContent().toString());
/* 103:107 */     } else if ((item instanceof CommentNode)) {
/* 104:108 */       writer.writeComment(((CommentNode)item).getContent().toString());
/* 105:109 */     } else if (!(item instanceof EndTagToken)) {
/* 106:111 */       if ((item instanceof TagNode)) {
/* 107:112 */         serialize((TagNode)item, writer);
/* 108:    */       }
/* 109:    */     }
/* 110:    */   }
/* 111:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.html.htmlcleaner.XmlSerializer
 * JD-Core Version:    0.7.0.1
 */